﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class CreatePrivateForum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.CreatePrivateForum_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            //this.Title = GND.ReturnAppTitle();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (!GND.ReadNivdalBoolData("@UserId", "Nivdal_ForumCanUserCreatePrivateForum", ou.UserId))
                Response.Redirect("~/ForumHomePage.aspx");

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

        }
        protected void CreateNewForumButton_Click(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            String ForumName = this.ForumNameTextBox.Text.Trim();
            String PublicInfo = this.PublicInfoTextArea.Text.Trim();
            String PrivateInfo = this.PrivateInfoTextArea.Text.Trim();

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumAddNewForum";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = ou.UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_ForumName = new SqlParameter();
            dbParam_ForumName.ParameterName = "@ForumName";
            dbParam_ForumName.Value = ForumName;
            dbParam_ForumName.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumName);

            SqlParameter dbParam_PublicInfo = new SqlParameter();
            dbParam_PublicInfo.ParameterName = "@PublicInfo";
            dbParam_PublicInfo.Value = PublicInfo;
            dbParam_PublicInfo.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_PublicInfo);

            SqlParameter dbParam_PrivateInfo = new SqlParameter();
            dbParam_PrivateInfo.ParameterName = "@PrivateInfo";
            dbParam_PrivateInfo.Value = PrivateInfo;
            dbParam_PrivateInfo.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_PrivateInfo);

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = "";
            dbParam_ForumId.DbType = DbType.Decimal;
            dbParam_ForumId.Direction = ParameterDirection.Output;
            dbParam_ForumId.Size = 12;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 128;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                if (dbParam_ForumId.Value.ToString() != "0")
                    Response.Redirect("ForumDetails.aspx?ForumId=" + dbParam_ForumId.Value.ToString() + "&Answer=" + dbParam_Answer.Value.ToString());
                else
                    this.ErrorLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.ErrorLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}